home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / mail / pine3.96.tar.gz / pine3.96.tar / pine3.96 / imap / non-ANSI / c-client / strtol.c < prev    next >
C/C++ Source or Header  |  1993-11-12  |  4KB  |  113 lines

  1. /*
  2.  * Program:    String to long
  3.  *
  4.  * Author:    Mark Crispin
  5.  *        Networks and Distributed Computing
  6.  *        Computing & Communications
  7.  *        University of Washington
  8.  *        Administration Building, AG-44
  9.  *        Seattle, WA  98195
  10.  *
  11.  * Date:    11 May 1989
  12.  * Last Edited:    12 November 1993
  13.  *
  14.  * Copyright 1993 by the University of Washington
  15.  *
  16.  *  Permission to use, copy, modify, and distribute this software and its
  17.  * documentation for any purpose and without fee is hereby granted, provided
  18.  * that the above copyright notice appears in all copies and that both the
  19.  * above copyright notice and this permission notice appear in supporting
  20.  * documentation, and that the name of the University of Washington not be
  21.  * used in advertising or publicity pertaining to distribution of the software
  22.  * without specific, written prior permission.  This software is made
  23.  * available "as is", and
  24.  * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
  25.  * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
  26.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
  27.  * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
  28.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  29.  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  30.  * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
  31.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  32.  *
  33.  */
  34.  
  35. /*
  36.  * Turn a string long into the real thing (swiped from KCC)
  37.  * Accepts: source string
  38.  *        pointer to place to return end pointer
  39.  *        base
  40.  * Returns: parsed long integer, end pointer is updated
  41.  */
  42.  
  43. long strtol (s,endp,base)
  44.      char *s;
  45.      char **endp;
  46.      int base;
  47. {
  48.   long value = 0;        /* the accumulated value */
  49.   int negative = 0;        /* this a negative number? */
  50.   int ok;            /* true while valid chars for number */
  51.   char c;
  52.                 /* insist upon valid base */
  53.   if (base && (base < 2 || base > 36)) return NIL;
  54.   while (isspace (*s)) s++;    /* skip leading whitespace */
  55.   if (!base) {            /* if base = 0, */
  56.     if (*s == '-') {        /* integer constants are allowed a */
  57.       negative = 1;        /* leading unary minus, but not */
  58.       s++;            /* unary plus. */
  59.     }
  60.     else negative = 0;
  61.     if (*s == '0') {        /* if it starts with 0, its either */
  62.                 /* 0X, which marks a hex constant, */
  63.       if (toupper (*++s) == 'X') {
  64.     s++;            /* skip the X */
  65.            base = 16;        /* base is hex 16 */
  66.       }
  67.       else base = 8;        /* leading 0 means octal number */
  68.     }
  69.     else base = 10;        /* otherwise, decimal. */
  70.     do {
  71.       switch (base) {        /* char has to be valid for base */
  72.       case 8:            /* this digit ok? */
  73.     ok = isodigit(*s);
  74.     break;
  75.       case 10:
  76.     ok = isdigit(*s);
  77.     break;
  78.       case 16:
  79.     ok = isxdigit(*s);
  80.     break;
  81.       default:            /* it's good form you know */
  82.     return NIL;
  83.       }
  84.                 /* if valid char, accumulate */
  85.       if (ok) value = value * base + toint(*s++);
  86.     } while (ok);
  87.     if (toupper(*s) == 'L') s++;/* ignore 'l' or 'L' marker */
  88.     if (endp) *endp = s;    /* set user pointer to after num */
  89.     return (negative) ? -value : value;
  90.   }
  91.  
  92.   switch (*s) {            /* check for leading sign char */
  93.   case '-':
  94.     negative = 1;        /* yes, negative #.  fall into '+' */
  95.   case '+':
  96.     s++;            /* skip the sign character */
  97.   }
  98.                 /* allow hex prefix "0x" */
  99.   if (base == 16 && *s == '0' && toupper (*(s + 1)) == 'X') s += 2;
  100.   do {
  101.                 /* convert to numeric form if digit*/
  102.     if (isdigit (*s)) c = toint (*s);
  103.                 /* alphabetic conversion */
  104.     else if (isalpha (*s)) c = *s - (isupper (*s) ? 'A' : 'a') + 10;
  105.     else break;            /* else no way it's valid */
  106.     if (c >= base) break;    /* digit out of range for base? */
  107.     value = value * base + c;    /* accumulate the digit */
  108.   } while (*++s);        /* loop until non-numeric character */
  109.   if (endp) *endp = s;        /* save users endp to after number */
  110.                 /* negate number if needed */
  111.   return (negative) ? -value : value;
  112. }
  113.